package dp;

public class leetCode845_longestMountain {

    public int longestMountain(int[] arr) {
        if(arr == null || arr.length < 3 )return 0;
        int n = arr.length;
        int[] left = new int[n];
        int[] right = new int[n];
        int ans = 0;
        left[0] = 0;

        for (int i = 1; i < n; i++) {
            if(arr[i] > arr[i - 1]){
                left[i] = left[i - 1] + 1;
            }
        }

        right[n - 1] = 0;
        for (int i = n - 2; i >= 0 ; i--) {
            if(arr[i] > arr[i + 1]){
                right[i] = right[i + 1] + 1;
            }
        }
        for (int i = 0; i < n; i++) {
            if(left[i] > 0 && right[i] > 0) {
                ans = Math.max(ans, left[i] + right[i] + 1);
            }
        }
        return ans;
    }
}
